package com.zm.test;

import com.zm.dao.IAccountDao;
import com.zm.dao.IUserDao;
import com.zm.domain.Account;
import com.zm.domain.AccountUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisAccount {
    private InputStream in;
    private SqlSession sqlSession;
    private IAccountDao accountDao;

    @Before //用于在测试方法之前执行
    public void init() throws IOException {
        //1.读取配置文件，生成字节输入流
        in= Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory\
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
        //3.获取SqlSession对象
        sqlSession=factory.openSession(true);//事务自动提交
        //4.获取代理对象
        accountDao=sqlSession.getMapper(IAccountDao.class);
    }
    @After //用于在测试方法之后执行
    public  void destroy() throws IOException {
        sqlSession.close();
        in.close();
    }
    @Test
    public void testFindAll(){
        List<Account> accounts=accountDao.findAll();
        for (Account account : accounts){
            System.out.println("===============================每个account的信息=============================");
            System.out.println(account);
            System.out.println("-----------------------------------------------------------");
            System.out.println(account.getUser());
        }
    }
    @Test
    public void testFindAllAccount(){
        List<AccountUser> aus=accountDao.findAllAccount();
        for (AccountUser au : aus){
            System.out.println(au);
        }
    }

}
